/**
 * 表单验证工具函数
 * 提供通用的表单字段验证逻辑
 */

/**
 * 验证必填字段
 * @param value 字段值
 * @param fieldName 字段名称
 * @returns 验证结果
 */
export const validateRequired = (value: any, fieldName: string): { valid: boolean; message?: string } => {
  if (!value || (typeof value === 'string' && value.trim() === '')) {
    return {
      valid: false,
      message: `请填写${fieldName}`
    };
  }
  return { valid: true };
};

/**
 * 验证金额字段
 * @param amount 金额值
 * @param fieldName 字段名称（默认为"金额"）
 * @returns 验证结果
 */
export const validateAmount = (amount: any, fieldName: string = '金额'): { valid: boolean; message?: string } => {
  // 先验证必填
  const requiredCheck = validateRequired(amount, fieldName);
  if (!requiredCheck.valid) {
    return requiredCheck;
  }

  // 验证是否为有效数字
  const numAmount = Number(amount);
  if (isNaN(numAmount) || numAmount <= 0) {
    return {
      valid: false,
      message: `请输入有效的${fieldName}`
    };
  }

  return { valid: true };
};

/**
 * 验证日期字段
 * @param date 日期值
 * @param fieldName 字段名称（默认为"日期"）
 * @returns 验证结果
 */
export const validateDate = (date: any, fieldName: string = '日期'): { valid: boolean; message?: string } => {
  // 先验证必填
  const requiredCheck = validateRequired(date, fieldName);
  if (!requiredCheck.valid) {
    return requiredCheck;
  }

  // 验证日期格式
  const dateObj = new Date(date);
  if (isNaN(dateObj.getTime())) {
    return {
      valid: false,
      message: `请选择有效的${fieldName}`
    };
  }

  return { valid: true };
};

/**
 * 验证选择字段（下拉框、单选等）
 * @param value 选择值
 * @param fieldName 字段名称
 * @returns 验证结果
 */
export const validateSelect = (value: any, fieldName: string): { valid: boolean; message?: string } => {
  if (!value || value === '' || value === null || value === undefined) {
    return {
      valid: false,
      message: `请选择${fieldName}`
    };
  }
  return { valid: true };
};

/**
 * 批量验证表单字段
 * @param validations 验证规则数组
 * @returns 验证结果
 */
export const validateForm = (validations: Array<{ valid: boolean; message?: string }>): { valid: boolean; message?: string } => {
  for (const validation of validations) {
    if (!validation.valid) {
      return validation;
    }
  }
  return { valid: true };
};

/**
 * 合同表单验证
 * @param formData 表单数据
 * @returns 验证结果
 */
export const validateContractForm = (formData: {
  contractName: string;
  contractAmount: any;
  contractDate: any;
  contractType: any;
  contractCompany: string;
  contractContent: string;
}): { valid: boolean; message?: string } => {
  return validateForm([
    validateRequired(formData.contractName, '合同名称'),
    validateAmount(formData.contractAmount, '合同金额'),
    validateDate(formData.contractDate, '合同日期'),
    validateSelect(formData.contractType, '合同类型'),
    validateRequired(formData.contractCompany, '合同公司'),
    validateRequired(formData.contractContent, '合同内容')
  ]);
};

/**
 * 付款表单验证
 * @param formData 表单数据
 * @returns 验证结果
 */
export const validatePaymentForm = (formData: {
  payAmount: any;
  payDate: any;
  payObject: string;
  payReason: string;
}): { valid: boolean; message?: string } => {
  return validateForm([
    validateAmount(formData.payAmount, '付款金额'),
    validateDate(formData.payDate, '付款日期'),
    validateRequired(formData.payObject, '付款对象'),
    validateRequired(formData.payReason, '付款事由')
  ]);
};

/**
 * 报销表单验证
 * @param formData 表单数据
 * @returns 验证结果
 */
export const validateExpenseForm = (formData: {
  expenseAmount: any;
  expenseDate: any;
  expenseType: any;
}): { valid: boolean; message?: string } => {
  return validateForm([
    validateAmount(formData.expenseAmount, '报销金额'),
    validateDate(formData.expenseDate, '报销日期'),
    validateSelect(formData.expenseType, '报销类型')
  ]);
};